$(function(){
    //页面加载完成之后执行
    checkAlarm();
});
var mappings=[];
function getmappings(){
    $.ajax({
        type : "get",
        url : "mappings",
        dataType : "json",
        async:false,
        data : {},
        success : function(data) {
            mappings=data;
        },error:function(errmsg){
            alert("Ajax获取服务器数据出错了！"+ errmsg);
        }
    });
}

var cars=[];
function checkAlarm() {
    $.ajax({
        type : "get",
        url : "checkAlarm",
        dataType : "json",
        data : {},
        success : function(data) {
            for (var p in data) {
                cars.push(data[p].carId);
            }
            alertChart();
        },error:function(errmsg){
            alert("Ajax获取服务器数据出错了！"+ errmsg);
        }
    });
}
function alertChart() {
    var myChart = echarts.init(document.getElementById('alert'));
    option = {
        backgroundColor: '#FFFFFF',
        title: {
            text: '报警情况',
            left: 'center',
            top: 20,
            textStyle: {
                color: '#ccc'
            }
        },
        tooltip : {
            trigger: 'item',
            formatter: "{a}"
        },
        series : initSeries()
    };
    myChart.setOption(option);
}

function initSeries(){
    var mySeries = [];
    getmappings();
    console.log(cars);
    for (var i=0;i<mappings.length;i++){
        var carId=mappings[i].carId;
        var center=[(i%10)*10+5+"%",parseInt(i/10)*15+10+"%"];
        var color=cars.indexOf(carId)>-1?"#C20B1B":"#1ac22a";
        var name=cars.indexOf(carId)>-1?"报警":"正常";
        var serie={
            name:name,
            type:'pie',
            radius : '8%',
            center: center,
            data:[
                {value:100,name:"车编号"+mappings[i].carId}
            ],
            labelLine: {
                show:false
            },
            label: {
                show:true
            },
            itemStyle: {
                normal: {
                    color: color,
                }
            }
        };
        mySeries.push(serie);
    }
    return mySeries;
}